/*
** commandes.c for  in /home/gomes_m//my-project-a-moi/server
** 
** Made by mickael gomes
** Login   <gomes_m@epitech.net>
** 
** Started on  Tue Jul 23 15:49:56 2013 mickael gomes
** Last update Tue Jul 23 17:11:36 2013 mickael gomes
*/

#include		<stdio.h>
#include		<unistd.h>
#include		<sys/wait.h>
#include		"struct.h"
#include		"prototypes.h"

void			my_ls(t_list *list)
{
  int			pipefd[2];
  int			status;
  int			son_pid;

  list->cmd->result = xmalloc(1024 * sizeof(char));
  pipe(pipefd);
  son_pid = fork();
  /* Faire une méga fonction pour gere tout les cas de retours foireux de Wait(). */
  wait(&status);
  if (son_pid == 0)
    {
      close(pipefd[0]);
      dup2(pipefd[1], 1);
      list->cmd->pos = -1;
      execlp("ls", "ls", NULL);
    }
  else
    {
      int			len;
      
      close(pipefd[1]);
      len = read(pipefd[0], list->cmd->result, 1024);
      list->cmd->result[len] = 0;
      len = xwrite(list->fd, list->cmd->result, my_strlen(list->cmd->result));
      if (len != my_strlen(list->cmd->result))
        xwrite(list->fd, &list->cmd->result[len], my_strlen(list->cmd->result) - len);
    }
}
